package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 15/6/25.
 */
public class ShowPushInfosActivity extends BaseUIActivity implements GetServicesDataUtil.IGetServicesDataCallBack {

	@ViewInject(id = R.id.push_name)
	private TextView push_name;
	@ViewInject(id = R.id.goods_describe)
	private TextView goods_describe;
	@ViewInject(id = R.id.create_time)
	private TextView create_time;

	@ViewInject(id=R.id.seeCommentsDetail_tv,click = "seeCommentsDetail_tv_click")
	private TextView seeCommentsDetail_tv;

	private JsonMap map;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getIntent() == null) {
			toast.showToast(R.string.msg_server_error);
			finish();
			return;
		} else {
			setContentView(R.layout.show_goodsinfos_layout);
			setCenter_title(R.string.push_detail);
			initViewData();
		}
        setResult(RESULT_OK);
	}

	private void initViewData() {
		String extra = getIntent().getStringExtra("data");
		map = JsonParseHelper.getJsonMap(extra);
		int pushType=map.getInt("PushType",0);
		if(pushType==0) {
			seeCommentsDetail_tv.setVisibility(View.GONE);
		} else if (pushType == 1) {
			seeCommentsDetail_tv.setVisibility(View.VISIBLE);
		}

		push_name.setText(map.getStringNoNull("Title"));
		goods_describe.setText("\t\t\t\t"+map.getStringNoNull("Content"));
		// goods_describe.loadData(map.getString("Content"),"text/html","utf-8");
		create_time.setText(map.getString("CreateTime"));
		if (!map.getBoolean("IsRead")) {  //未读
			setRead(map.getStringNoNull("poId"));
            sendBroadcast(new Intent(Constant.ACAT_SHOPCARTNUMCHANGE));
        }
	}

	private void setRead(String id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("poId", id);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_GetUpdPushMessage);
		queue.setParamsNoJson(params);
		queue.setWhat(GetDataConfing.What_GetUpdPushMessage);
		queue.setCallBack(new GetServicesDataUtil.IGetServicesDataCallBack() {
			@Override
			public void onLoaded(GetServicesDataQueue entity) {
                LogUtil.d(TAG,entity.getInfo());
			}
		});
		getDataUtil.getData(queue);
	}

	private void getGoodsInfs() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("PushMessageId", getIntent().getStringExtra("PUSHMSGID"));
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_GetPushMessageDetails);
		queue.setParamsNoJson(params);
		queue.setCallBack(this);
	}

	@Override
	public void onLoaded(GetServicesDataQueue entity) {
		if (entity.isOk()) {
			JsonMap map = JsonParseHelper.getJsonMap(entity.getInfo());
			push_name.setText(map.getString("Title"));
			// goods_describe.loadData(map.getString("Content"));
			create_time.setText(map.getString("CreateTime"));
		} else {
			ShowGetDataError.showNetError(this);
			finish();
		}
		loadingToast.dismiss();
	}

	/**
	 * 馋看品论回复
	 * @param view
	 */
	public void seeCommentsDetail_tv_click(View view){
		Intent intent=new Intent(this,FindCommentsActivity.class);
		intent.putExtra(ExtraKeys.Key_Msg1, map.getStringNoNull("P1"));
		intent.putExtra(ExtraKeys.Key_Msg2, getString(R.string.find_detail_all_comments));
		intent.putExtra(ExtraKeys.Key_Msg4,map.getStringNoNull("P2"));
		intent.putExtra(ExtraKeys.Key_Msg3,1);
        setResult(RESULT_OK);
		jumpTo(intent, true);
	}

}